// 用户相关接口
import ajax from '@util/ajax.ts'
import { Result } from '@src/interfaces/TypeResult.ts'
import { TypeUser } from '@src/interfaces/TypeUser.ts'
import { TypePageDTO } from '@src/interfaces/TypePageDTO.ts'

export async function getUser() {
  return await ajax.get('/user/getCurrentUser')
}

export async function join(id: number) {
  return await ajax.put(`/class-info/join/${id}`)
}

export async function exit() {
  return await ajax.put(`/class-info/exit`)
}

/**
 * 用户注册
 */
export function userRegisterApi(userInfo: TypeUser) {
  return ajax.post('/user', userInfo).then((res) => {
    return res.data as Result<any>
  })
}

/**
 * 用户登录
 */
export function userLoginApi(userInfo: TypeUser) {
  return ajax.post('/user/login', userInfo).then((res) => {
    return res.data as Result<string>
  })
}

/**
 * 更新用户信息
 */
export function updateUserApi(userInfo: TypeUser) {
  return ajax.put('/user/update', userInfo).then((res) => {
    return res.data as Result<string>
  })
}

/**
 * 分页+模糊查询获取用户列表
 */
export function getUserListApi(params: {
  page: number
  size: number
  name?: string
  account?: string
  phone?: string
}) {
  return ajax
    .get('/user/page', {
      params: params,
    })
    .then(({ data }) => {
      return data as Result<TypePageDTO<TypeUser>>
    })
}

/**
 * 解封用户
 * @param id
 */
export function enableUserApi(id: number) {
  return ajax.put(`/user/enable/${id}`).then((res) => {
    return res.data as Result<any>
  })
}

/**
 * 封禁用户
 */
export function disableUserApi(id: number) {
  return ajax.put(`/user/disable/${id}`).then((res) => {
    return res.data as Result<any>
  })
}

/**
 * 刷新用户token
 */
export function refreshTokenApi() {
  return ajax.get('/user/token/refresh').then((res) => {
    return res.data as Result<string>
  })
}

/**
 * 修改班级管理员
 */
export function changeClassAdminApi(userId: number) {
  return ajax.put(`/user/class-admin/${userId}`).then((res) => {
    return res.data as Result<null>
  })
}
